<?php
namespace gnomephp\mvc;

use gnomephp\Configuration,
	gnomephp\Url,
	gnomephp\message\Message;
class CoreView extends View{
	
	
	/**
	 * Sends a specific view to the output.
	 * 
	 * @param string $viewName The view name, Can be in folder too, like template/header, . This will load view/template/header.php
	 * @param boolean $returnContent If you want to return the rendered output instead of echoing it out, you may set this to true.
	 * @param string $suffix The file extension of the view file. Default is .php.
	 */
	public function render($viewName, $returnContent=false, $suffix='.php'){
		
		
		// Assign variables to nice onces... 
		extract($this->variables);
		
		$this->viewname = $viewName;
		$vpath = \gnomephp\Gnomephp::getResourceOverride('view' . DIRECTORY_SEPARATOR.$viewName.($suffix ?: $suffix));
		
		$view = $this;
		
		

		ob_start();
		
		// Include view.
		require $vpath;
	
		$content = ob_get_contents();
		ob_end_clean();	
		if ($this->extendingView !== null){
			$v = new CoreView($this->input, $this->lang, $this->session);
			$v->addImplementation($this->extendingViewAlias, $content);
			foreach($this->extendingVars as $k => $va){
				$v->assign($k, $va);
			}
			return $v->render($this->extendingView, $returnContent, $suffix);
		}
			
		if ($returnContent){
			return $content;
		}else{
			echo $content;
		}
		
						
	}
		
	/**
	 * 
	 * Imports a view and creates a separate view for the specific view. Useful to use inside templates like:
	 * <? $view->import('header'); ?>
	 * 
	 * @param string $view The view name, Can be in folder too, like template/header, . This will load view/template/header.php
	 * @param array $vars Array of variables to assign to this specific view. Key should be the variable name, Value can be anything.
	 * @param string $suffix The file extension of the view file. Default is .php.
	 */
	public function import($view, $vars=array(), $returnContent=false,  $suffix='.php'){
		$v = new CoreView($this->input, $this->lang, $this->session);
		foreach($vars as $k => $va){
			$v->assign($k, $va);
		}
		$v->render($view, $returnContent, $suffix);
	}
	
}